package com.tian.service.impl;

import com.alibaba.fastjson.JSON;
import com.tian.common.CommonResult;
import com.tian.dto.PaymentChannel4ApiRespDto;
import com.tian.dto.PaymentChannelDto;
import com.tian.entity.PaymentChannel;
import com.tian.mapper.PaymentChannelMapper;
import com.tian.service.PaymentChannelService;
import com.tian.util.PayRedisPrefixUtil;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * {@code @description:} TODO
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-11-23 9:26
 * {@code @version:} 1.0
 */
@Service
public class PaymentChannelServiceImpl implements PaymentChannelService {
    @Resource
    private PaymentChannelMapper paymentChannelMapper;
    @Resource
    private RedissonClient redissonClient;
    @Override
    public CommonResult<Boolean> addPaymentChannel(PaymentChannelDto paymentChannelDto) {
        PaymentChannel paymentChannel = new PaymentChannel();
        BeanUtils.copyProperties(paymentChannelDto, paymentChannel);
        paymentChannelMapper.insert(paymentChannel);
        redissonClient.getBucket("channel_" + paymentChannel.getId()).set(JSON.toJSONString(paymentChannel));
        cacheDataList();
        return CommonResult.success(Boolean.TRUE);
    }

    private void cacheDataList() {
        List<PaymentChannel> paymentChannels = paymentChannelMapper.selectAll();
        RList<String> list = redissonClient.getList(PayRedisPrefixUtil.PAY_CHANNEL_CACHE);
        list.clear();
        paymentChannels.forEach(item -> {
            list.add(JSON.toJSONString(item));
        });
    }

    @Override
    public CommonResult<Boolean> updatePaymentChannel(PaymentChannelDto paymentChannelDto) {
        PaymentChannel paymentChannel = new PaymentChannel();
        BeanUtils.copyProperties(paymentChannelDto, paymentChannel);
        paymentChannelMapper.updateByPrimaryKey(paymentChannel);
        cacheDataList();
        return CommonResult.success(Boolean.TRUE);
    }

    @Override
    public CommonResult<List<PaymentChannel4ApiRespDto>> getAllPaymentChannel() {
         RList<String> list = redissonClient.getList(PayRedisPrefixUtil.PAY_CHANNEL_CACHE);
         if(list.isEmpty()){
             cacheDataList();
         }
         List<PaymentChannel4ApiRespDto> paymentChannelDtoList=new ArrayList<>();
         list.forEach(item -> {
             PaymentChannel paymentChannel = JSON.parseObject(item, PaymentChannel.class);
             PaymentChannel4ApiRespDto paymentChannelDto = new PaymentChannel4ApiRespDto();
             paymentChannelDto.setValue(paymentChannel.getId());
             paymentChannelDto.setText(paymentChannel.getChannelName());

            paymentChannelDtoList.add(paymentChannelDto);
         });
        return CommonResult.success(paymentChannelDtoList);
    }
}
